package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.*;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.*;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.lang.model.element.Name;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@Controller
public class WebController{
    private static Logger log= LoggerFactory.getLogger(WebController.class);
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebNavigationService webNavigationService;
    @Resource
    private WebCooperateService webCooperateService;
    @Resource
    private WebCatalogService webCatalogService;
    @Resource
    private InformationService informationService;
    @Resource
    private WebDetailService webDetailService;
    @Resource
    private BannerService bannerService;
    @Resource
    private ProductService productService;
    @Resource
    private ArticleService articleService;
    /**
     * 首页
     * */
    @RequestMapping(value="/")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
        ModelAndView modelAndView=new ModelAndView("/web/index");
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        List<WebCooperate> list=webCooperateService.list(new WebCooperate());
        List<Information> informationList=informationService.list(new Information("","1","1"));

        modelAndView.addObject("informationList",informationList);

        modelAndView.addObject("webCooperateList",list);
        return modelAndView;
    }
    /**
     * 服务
     * */
    @RequestMapping(value="/web/service")
    public ModelAndView product(HttpServletRequest request,HttpServletResponse response,String menuId){
        ModelAndView modelAndView=new ModelAndView("/web/service");
        modelAndView.addObject("menuId",menuId);
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        return modelAndView;
    }

    /**
     * 新闻地址
     * */
    @RequestMapping(value="/web/news")
    public ModelAndView news(HttpServletRequest request,HttpServletResponse response,String menuId,Information information){
        ModelAndView modelAndView=new ModelAndView("/web/news");
        modelAndView.addObject("menuId",menuId);
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        information.setStatus("1");
        List<Information> informationList=informationService.list(information);
        modelAndView.addObject("informationList",informationList);
        information.setStatus("1");
        information.setShowFrist("1");
        modelAndView.addObject("informationNewList",informationService.list(information));
        return modelAndView;
    }
    /**
     * 新闻详情
     * */
    @RequestMapping(value="/web/news/{id}")
    public ModelAndView newsDetail(HttpServletRequest request,HttpServletResponse response,@PathVariable("id") String id,String menuId){
        ModelAndView modelAndView=new ModelAndView("/web/newsDetail");
        modelAndView.addObject("menuId",menuId);
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        modelAndView.addObject("information",informationService.selectByPrimaryKey(id));
        return modelAndView;
    }

    /**
     * 经典案例
     * */
    @RequestMapping(value="/web/case")
    public ModelAndView caseContent(HttpServletRequest request,HttpServletResponse response,String menuId){
        ModelAndView modelAndView=new ModelAndView("/web/case");
        modelAndView.addObject("menuId",menuId);
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        List<WebCooperate> list=webCooperateService.list(new WebCooperate());
        modelAndView.addObject("webCooperateList",list);
        return modelAndView;
    }
    /**
     * 联系我们
     * */
    @RequestMapping(value="/web/likeMe")
    public ModelAndView likeMe(HttpServletRequest request,HttpServletResponse response,String menuId){
        ModelAndView modelAndView=new ModelAndView("/web/likeMe");
        modelAndView.addObject("menuId",menuId);
        modelAndView.addObject("webConfig",acceptWebConfig());
        modelAndView.addObject("bannerList",acceptBannerList());
        modelAndView.addObject("navigationList",acceptWebNavigation());
        return modelAndView;
    }
    /**
     * 获取web基础配置
     */
    public WebConfig acceptWebConfig(){
       List<WebConfig> list = webConfigService.selectAll();
       if(list!=null&&list.size()>0){
           return list.get(0);
       }else{
           return null;
       }
    }
    /**
     * 获取web导航配置
     */
    public List<WebNavigation> acceptWebNavigation(){
        List<WebNavigation> webNavigationList = webNavigationService.list(new WebNavigation());
        List<WebNavigation> resultList =new ArrayList<>();
        if(webNavigationList!=null && webNavigationList.size() > 0){
            for(WebNavigation webNavigation:webNavigationList){
                if(BaseConstant.MENU_ONE.equals(webNavigation.getGrade())){
                    if("1".equals(webNavigation.getType())){
                        //目录
                        List<WebNavigation> resultLists =new ArrayList<>();
                        for(WebNavigation webNavigation1:webNavigationList){
                            if(webNavigation.getId().equals(webNavigation1.getParentId())){
                                resultLists.add(webNavigation1);
                            }
                        }
                        webNavigation.setWebNavigationList(resultLists);
                    }
                    resultList.add(webNavigation);
                }
            }
        }
        return resultList;
    }
    /**
     * 获取banner配置
     */
    public List<Banner> acceptBannerList(){
        return bannerService.list(new Banner(BaseConstant.BANNER_IS_WEB,BaseConstant.STATUS_IS_YES));
    }

    /**
     * 获取产品
     */
    public List<Product> acceptProductList(Product product){
        return productService.list(product);
    }
}
